package com.kf.smartpark.splash

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.alibaba.android.arouter.launcher.ARouter
import com.kf.common.mvi.view.BaseActivity
import com.kf.smartpark.databinding.ActivitySplashBinding
import kotlinx.coroutines.launch

class SplashActivity : BaseActivity<ActivitySplashBinding,SplashViewModel>(){
    override fun createViewModel(): SplashViewModel {
        return viewModels<SplashViewModel>().value
    }

    override fun createViewBind(): ActivitySplashBinding {
        return ActivitySplashBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindUiState()
        mViewModel.splashTime()
    }

    private fun bindUiState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                mViewModel.mutableContainer.uiStateFlow.collect{
                    mView.splashTime.setText(it.entity.values!!.str)
                    if (it.entity!!.values!!.str.equals("0s")){
                        ARouter.getInstance().build("/user/loginactivity").navigation()
                    }
                }
            }
        }
    }
}